document.addEventListener("DOMContentLoaded", function () {
    const fieldNameInput = document.getElementById("fieldName");
    const fieldTypeSelect = document.getElementById("fieldType");
    const addButton = document.getElementById("addConfig");
    const configList = document.getElementById("configList");
    const exportButton = document.getElementById("exportConfig");
    const importButton = document.getElementById("importConfigBtn");
    const importInput = document.getElementById("importConfig");

    // 复选框
    const fillPdfCheckbox = document.getElementById('fillPdfCheckbox');
    const fillDropdownCheckbox = document.getElementById('fillDropdownCheckbox');

    // 复选框状态变化处理
    fillPdfCheckbox.addEventListener("change", function () {
        chrome.storage.local.set({ fillPdf: this.checked }, function () {
            // 刷新当前页面
            chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
                chrome.tabs.reload(tabs[0].id);  // 刷新当前页面
            });
        });
    });

    fillDropdownCheckbox.addEventListener("change", function () {
        chrome.storage.local.set({ fillDropdown: this.checked }, function () {
            // 刷新当前页面
            chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
                chrome.tabs.reload(tabs[0].id);  // 刷新当前页面
            });
        });
    });


    // 读取存储的配置并显示
    function loadConfig() {
        chrome.storage.local.get(["config"], function (result) {
            configList.innerHTML = "";
            let configData = result.config || [];
            configData.forEach((item, index) => {
                let li = document.createElement("li");
                li.textContent = `${item.name} - ${item.type}`;
                let delBtn = document.createElement("button");
                delBtn.textContent = "删除";
                delBtn.style.marginLeft = "10px";
                delBtn.onclick = function () {
                    removeConfig(index);
                };
                li.appendChild(delBtn);
                configList.appendChild(li);
            });
        });
    }

    // 添加配置
    addButton.addEventListener("click", function () {
        let name = fieldNameInput.value.trim();
        let type = fieldTypeSelect.value;

        if (!name) {
            alert("请输入字段名称");
            return;
        }

        chrome.storage.local.get(["config"], function (result) {
            let configData = result.config || [];
            configData.push({ name, type });
            chrome.storage.local.set({ config: configData }, function () {
                fieldNameInput.value = "";
                loadConfig();
            });
        });
    });

    // 删除配置
    function removeConfig(index) {
        chrome.storage.local.get(["config"], function (result) {
            let configData = result.config || [];
            configData.splice(index, 1);
            chrome.storage.local.set({ config: configData }, function () {
                loadConfig();
            });
        });
    }

    // 导出配置为 JSON 文件并添加注释
    exportButton.addEventListener("click", function () {
        chrome.storage.local.get(["config"], function (result) {
            let configData = result.config || [];
            const jsonContent = JSON.stringify(configData, null, 2);
            const blob = new Blob([jsonContent], { type: "application/json" });
            const url = URL.createObjectURL(blob);
            const link = document.createElement("a");
            link.href = url;
            link.download = "config.json";
            link.click();
            URL.revokeObjectURL(url); // 释放对象URL
        });
    });

    // 导入配置从 JSON 文件
    importButton.addEventListener("click", function () {
        importInput.click();  // 点击隐藏的输入框
    });

    importInput.addEventListener("change", function (e) {
        const file = e.target.files[0];
        if (file && file.type === "application/json") {
            const reader = new FileReader();
            reader.onload = function (event) {
                try {
                    const importedConfig = JSON.parse(event.target.result);
                    chrome.storage.local.set({ config: importedConfig }, function () {
                        loadConfig();
                    });
                } catch (error) {
                    alert("导入的文件格式无效");
                }
            };
            reader.readAsText(file);
        } else {
            alert("请选择一个有效的 JSON 文件");
        }
    });

    // 页面加载时读取配置
    loadConfig();
});

